// pages/recruit/personal/resume/info/info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['江苏省', '南京市', '玄武区'],
    qualifications: ["初中","高中/职高","专科","本科","硕士","博士"],
    qualkey: 3,
    info: {
      avatar: "../../../../../images/head.jpg",
      name: "",
      sex: "MALE",
      province: "",
      city: "请填写",
      region: "",
      birthday: "请填写",
      height: "",
      educationStatus: "READING",
      education: "本科",
      tel: "",
      email: "",
      qq: "",
      wechat:""
    },
    qiniuToken: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    getApp().globalData.get("/api/resume",{}).then(res =>{
      if (res.data != null){
        that.setData({
          info: res.data.resume
        })
      }
    })
    //获取七牛云的token
    getApp().globalData.get("/api/qiniu/auth", {}).then(res => {
      if (res.data != null) {
        that.data.qiniuToken = res.data
      }
    })
  },
  chooseImage: function() {
    const that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success:function(e){
        wx.uploadFile({
          url: 'https://up-z2.qiniup.com',
          name: 'file',
          filePath: e.tempFilePaths[0],
          formData: {
            token: that.data.qiniuToken,
          },
          success: function (res) {
            console.log(res)
            let imgurl = "https://image.xuegongbang.cn/" + JSON.parse(res.data).key;
            that.data.info.avatar = imgurl;
            that.setData({
              info: that.data.info
            })
          },
          fail: function (res) {
            wx.showToast({
              title: '服务错误',
              icon: "warn",
            })
          }
        })
      }
    })
  },
  nameinput: function(e) {//名字选择
    const that = this;
    let reg = /^[\u4e00-\u9fa5]{0,}$/
    if (reg.test(e.detail.value)) {
      that.data.info.name = e.detail.value;
      that.setData({
        info: that.data.info,
      })
      return e.detail.value;
    } else {
      wx.showToast({
        title: '请输入汉字',
        icon: "none",
        duration: 1000
      })
      return ""
    }
  },
  sexselect: function(e) {//性别选择
    const that = this;
    that.data.info.sex = e.detail.value;
    that.setData({
      info: that.data.info,
    })
    console.log(that.data.info)
  },
  changecity: function (e) {//城市选择
    console.log(e.detail.value)
    const that = this;
    that.data.info.province = e.detail.value[0];
    that.data.info.city = e.detail.value[1];
    that.data.info.region = e.detail.value[2];
    that.setData({
      info: that.data.info,
      region: e.detail.value
    })
  },
  changebirthday: function (e){//生日选择
    const that = this;
    that.data.info.birthday = e.detail.value;
    that.setData({
      info: that.data.info,
    })
  },
  heightinput: function(e){//身高选择
    const that = this;
    that.data.info.height = e.detail.value;
    that.setData({
      info: that.data.info,
    })
  },
  eduselect: function(e){//教育状态选择
    console.log(e.detail.value)
    const that = this;
    that.data.info.educationStatus = e.detail.value;
    that.setData({
      info: that.data.info,
    })
  },
  qualifications: function (e) {//学历选择
    const that = this;
    that.data.info.education = that.data.qualifications[parseInt(e.detail.value)];
    that.setData({
      info: that.data.info,
      qualkey: parseInt(e.detail.value)
    })
  },
  changetel: function(e) {
    const that = this;
    let reg = /^1[3|4|5|7|8][0-9]{9}$/;
    if (e.detail.value.length == 11) {
      if (!reg.test(e.detail.value)) {
        wx.showToast({
          title: '您输入的手机号有误',
          icon: "none",
          duration: 1000
        })
      } else {
        that.data.info.tel = e.detail.value;
        that.setData({
          info: that.data.info,
        })
      }
    } else {
      that.data.info.tel = e.detail.value;
      that.setData({
        info: that.data.info,
      })
    }
  },
  changeemail: function (e) {
    const that = this;
    that.data.info.email = e.detail.value;
    that.setData({
      info: that.data.info,
    })
  },
  changeQQ: function(e){
    const that = this;
    that.data.info.qq = e.detail.value;
    that.setData({
      info: that.data.info,
    })
  },
  changeWX: function (e) {
    const that = this;
    that.data.info.wechat = e.detail.value;
    that.setData({
      info: that.data.info,
    })
  },
  back: function() {
    wx.navigateBack({})
  },
  commit: function() {
    const that = this;
    let reg = /^1[3|4|5|7|8][0-9]{9}$/;
    let regemail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var box = that.data.info;
    let title;
    console.log(that.data.info)
    if (box.name == ""){
      title = "姓名不能为空"
    } else if (box.city == "请填写"){
      title = "请选择所在城市"
    } else if (box.birthday == "请填写") {
      title = "请填写您的生日"
    } else if (box.tel == "") {
      title = "请填写手机号码"
    } else if (!reg.test(box.tel)){
      title = "手机号有误"
    }else {
      if (box.email != "" && !regemail.test(box.email)) {
          title = "您输入的邮箱有误"
      }else{
        getApp().globalData.post("/api/resume",  that.data.info ).then(res => {
          wx.navigateBack()
        })
        return;
      }
    }
    wx.showToast({
      title: title,
      icon: "none",
      duration: 1000
    })
  }
})